import { user_login } from "@/service/api/sp-common/auth";
import { setToken } from "@/utils/auth";
import store from "@/store";
const state = {
  app_token: null, //token
  expireTime: 0, //过期时间
  isAnonUser: false, //是否是匿名用户
  updatePubUserParams: {}, //手机号加密文-params:{encryptedData,ivStr}
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.app_token = token;
  },
  SET_EXPIRE_TIME: (state, data) => {
    state.expireTime = data;
  },
  SET_ANON_USER: (state, data) => {
    state.isAnonUser = data;
  },
  SET_UPDATE_PUB_USER: (state, data) => {
    state.updatePubUserParams = data;
  },
};
const actions = {
  async userLogin({ commit, dispatch, state }) {
    const login_res = await dispatch("loginAction", false);
    if (
      (login_res.code === 200 || login_res.code === 0) &&
      !login_res.data.anonUser
    ) {
      const {
        data: { expireTime, token },
      } = login_res;
      commit("SET_ANON_USER", false);
      commit("SET_EXPIRE_TIME", expireTime);
      commit("SET_TOKEN", token);
      setToken(token).then(() => {
        store.dispatch("sp-common/user/getUserBaseInfoAction"); //获取用户信息
      });
    } else if (login_res.data.anonUser) {
      commit("SET_TOKEN", login_res.data.token);
      await setToken(login_res.data.token);
      commit("SET_ANON_USER", true);
    }
    store.dispatch("sp-store/index/getIndustry");
  },
  //游客用户专用登录
  async anonUserLogin({ commit, dispatch }) {
    const login_res = await dispatch("loginAction", true);
    store.dispatch("sp-store/index/getIndustry");
    if (
      login_res.data.url &&
      login_res.data.type === "webview" &&
      login_res.code === 61000
    ) {
      commit("SET_ANON_USER", true);
      const {
        data: { url },
      } = login_res;
      uni.$u.route({
        url: "pages/sp-common/pages/h5-login/index",
        params: {
          webViewUrl: encodeURIComponent(url),
        },
      });
    }
  },
  //登录的操作
  loginAction({ commit, state, dispatch }, force = false) {
    return new Promise((resolve, reject) => {
      uni.login({
        scopes: "auth_base",
        success: async (result) => {
          const { code } = result;
          if (code) {
            const login_res = await user_login({
              code,
              scope: 0,
              webviewAuth: force,
              userType: 2,
            });
            if (login_res.code === 200 || login_res.code === 0) {
              resolve(login_res);
            } else if (login_res.code === 61000) {
              resolve(login_res);
            } else {
              reject(login_res);
            }
          }
          //
        },
        fail: (err) => {
          reject(err);
        },
      });
    });
  },
  //存updatePubUserParams
  setPubUserParams({ commit }, data) {
    commit("SET_UPDATE_PUB_USER", data);
  },
};

const getters = {
  isLogin(state) {
    return !!state.app_token;
  },
};

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations,
};
